package com.iscas.green.databasechaostest.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;

@Configuration
public class ClickHouseConfig {

    @Bean(name = "clickhouseDataSource", initMethod = "init", destroyMethod = "close")
    public DruidDataSource clickhouseDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        // 直接设置连接信息
        dataSource.setUrl("jdbc:clickhouse:http://10.147.245.34:8123/default");
        dataSource.setDriverClassName("com.clickhouse.jdbc.ClickHouseDriver");
        dataSource.setUsername("default");
        dataSource.setPassword("KiVvqRCwO5");

        // Druid连接池配置
        dataSource.setInitialSize(1);
        dataSource.setMinIdle(1);
        dataSource.setMaxActive(10);
        dataSource.setMaxWait(30000);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(true);

        return dataSource;
    }

    @Bean(name = "clickhouseJdbcTemplate")
    public JdbcTemplate clickhouseJdbcTemplate(
            @Qualifier("clickhouseDataSource") DruidDataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}